package com.zdp.provider.controller;

import com.zdp.provider.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

/**
 * 类描述: 教师信息服务
 *
 * @author ZhhengDP
 * @since 2021/8/16
 */
@RestController
@RequestMapping("/teacher_info")
public class TeacherController {

    private RestTemplate restTemplate;

    private TeacherService teacherService;

    @Autowired
    public TeacherController(RestTemplate restTemplate, TeacherService teacherService) {
        this.restTemplate = restTemplate;
        this.teacherService = teacherService;
    }

    @GetMapping("/teacher")
    public TeacherVo getTeacher(Integer teacherId){
         TeacherVo teacher = teacherService.getTeacherById(teacherId);
         List<CourseDto> courses = loadCourses(teacherId);
         teacher.setCourses(courses);
         return teacher;
    }

    @PostMapping("/demoGet")
    public void demoGet(){
    }


    protected List<CourseDto> loadCourses(Integer teacherId){
        String url = "http://127.0.0.1:9904/courseservice/course_info/courses?teacherId={teacherId}";
        HashMap<String,Object> param = new HashMap<>();
        param.put("teacherId",teacherId);
        List<CourseDto> result = restTemplate.getForObject(url, List.class, param);
        return result;
    }
}
